<template>
  <div class="login-container">
      <van-nav-bar
        title="登录"
        class="page-nav-bar"
      >
      <template #left>
        <van-icon @click="$router.bush('/')" color="#fff" name="cross" />
      </template>
      </van-nav-bar>
<!-- ref="from" 获取表单组件 -->
      <van-form @submit="onSubmit" ref="form">
          <van-field
            v-model="form.mobile"
            name="mobile"
            placeholder="请输入手机号"
            :rules='formRules.mobile'
            type="number"
            maxlength="11"
          >
          <template v-slot:left-icon>
            <i class=" toutiao toutiao-shouji">
            </i>
          </template>
          </van-field>
          <van-field
            v-model="form.code"
            name="code"
            placeholder="请输入验证码"
            :rules='formRules.code'
            type="number"
            maxlength="6"
          >
          <!-- 左侧图标 -->
          <template v-slot:left-icon>
            <i class=" toutiao toutiao-yanzhengma">
            </i>
          </template>
          <!-- 右侧按钮 -->
          <template #button>
            <van-count-down @finish='isShowCountDown=false' v-if="isShowCountDown" :time="60 *1000" format=" ss s" />
            <!-- native-type="button"防止触发表单提交  -->
            <van-button v-else @click="onSendCode" native-type="button" class="send-btn" size="mini" >发送验证码</van-button>
          </template>
          </van-field>

          <div class="login-btn" >
            <van-button round block type="info" native-type="submit">登录</van-button>
          </div>
      </van-form>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { sendCode, login } from '@/api/user.js'
const mobileReg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
export default {
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      // 表单验证规则
      formRules: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: mobileReg, message: '请输入正确手机号' }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /^[0-9]{6}$/, message: '请输入正确验证码' }
        ]
      },
      // 定义倒计时显示隐藏
      isShowCountDown: false
    }
  },

  created () {

  },

  methods: {
    async onSubmit () {
      try {
        // const { data: res } = await login(this.form)
        const res = await login(this.form)
        this.$store.commit('setUser', res)
        Toast.success('登录成功')
        this.$router.push('/profile')
      } catch (err) {
        if (err.response && err.response.status === 400) {
          return Toast.fail('验证码错误或重新登录')
        }
        Toast.fail('系统异常')
      }
    },
    async onSendCode () {
      // 先检验
      try {
        // await可以等this.$refs.form.validate('mobile')执行完再执行下面的代码
        // 单纯的校验手机号的规则
        await this.$refs.form.validate('mobile')
      } catch (err) {
        return
      }

      // 检验正确，做请求。 做成功的操作
      try {
        await sendCode(this.form.mobile)
        Toast.success('亲，发送成功')
        this.isShowCountDown = true
      } catch (err) {
        // console.dir(err)
        if (err.response && err.response.status === 429) {
          return Toast.fail('亲，操作频繁稍后重试')
        }
        Toast.fail('亲，软件异常')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login-container{
  .toutiao{
    font-size: 37px;
  }
  .send-btn {
    width: 152px;
    height: 46px;
    background: #EDEDED;
    border-radius: 23px;
  }
  .login-btn{
    margin: 53px 28px;
    .van-button{
      width: 694px;
      height: 88px;
      background: #6DB4FB;
      border-radius: 10px;
      border: none;
    }
  }
}
</style>
